import {
  BaseScene
} from "../object/baseScene";
import {
  ChessBoard
} from "../object/onLinechessBoard";


export class OnLineScene extends BaseScene {
  constructor() {
    super('OnLineScene')
  }
  init() {
    this.cameras.main.setBackgroundColor(0xcfe4f5)
    this.status = 0 // 游戏状态 0进行中 -1 结束

    this.isBlack = null
  }
  // 绘制棋盘
  _drawCheck() {
    this.chessBoard = new ChessBoard(this)
    this.chessBoard.draw()

    this.topCenterY = this.computedY(this.chessBoard.checkBg.height) / 2
    this.bottomCenterY = this.chessBoard.checkBg.height + this.topCenterY * 3
    this._drawText()
  }
  // 绘制描述信息
  _drawText() {
    this.backText = this.add.text(this.gameWidth - 100, this.topCenterY, '返回', {
      fontSize: 28,
      fontFamily: '楷体',
      color: '#000000'
    })
    this.backText.setOrigin(0, 0.5)
    this.backText.setInteractive()
    this.backText.on('pointerdown', () => {
      this.scene.switch('LoadScene')
      if (this.status == -1) {
        this.game.ws.send({
          func: 'room.exit'
        })
      }
    })
    this.game.ws.listen('room.exit', (res) => {
      this.game.Vue.notify(res.mes);
      this.game.account.chessBoard = false
    })


    this.topCodeText = this.add.text(30, this.topCenterY, '', {
      fontSize: 32,
      fontFamily: '楷体',
      color: '#000000'
    })
    this.topCodeText.setOrigin(0, 0.5)
    this.bottomCodeText = this.add.text(30, this.bottomCenterY, '', {
      fontSize: 32,
      fontFamily: '楷体',
      color: '#000000'
    })
    this.bottomCodeText.setOrigin(0, 0.5)

    this.backText = this.add.text(this.gameWidth - 100, this.bottomCenterY, '发送消息', {
      fontSize: 22,
      fontFamily: '楷体',
      color: '#000000'
    })
    this.backText.setOrigin(0, 0.5)
    this.backText.setInteractive()
    this.backText.on('pointerdown', () => {
      this.game.Vue.$prompt("请输入消息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^.{1,20}$/,
        inputErrorMessage: "限制20位",
      }).then(({
        value
      }) => {
        this.game.ws.send({
          func: "room.chat",
          title: '来自 ' + this.game.account.name + '的消息',
          message: value
        });
      });
    })
    this.game.ws.listen('room.chat', (res) => {
      this.game.Vue.notify(res.title, res.message, 3000);
    })

  }
  // 是否我方布棋
  isAllowCheck() {
    let sign = (this.chessData.now == this.isBlack) && this.status == 0
    return sign
  }
  // 更新描述文本
  updateText() {
    if (this.isAllowCheck()) {
      this.topCodeText.setText("对手正在观望")
      this.bottomCodeText.setText("我方布棋中...")
    } else {
      this.topCodeText.setText("对手布棋中...")
      this.bottomCodeText.setText("我方正在观望")
    }
  }
  // 布棋之后
  chessAfter() {
    this.chessData.now = this.chessData.now ? 0 : 1
    this.game.ws.send({
      func: "chessBoard.addRecord",
      tape: this.chessBoard.records[this.chessBoard.records.length - 1]
    })
    if (this.status == 0) this.updateText()
  }
  // ping值检测
  _drawPing() {
    this.pingText = this.add.text(this.gameWidth - 50, 20, this.game.ws.delay + 'ms', {
      fontSize: 14,
      fontFamily: '楷体',
      color: '#e60003'
    })
    this.game.ws.updatePingAfter = () => {
      this.pingText.setText(this.game.ws.delay + 'ms')
      if (this.game.ws.delay < 100) this.pingText.setColor('#59bf42')
      else if (this.game.ws.delay < 500) this.pingText.setColor('#b4d342')
      else this.pingText.setColor('#b92326')
      if (this.game.ws.delay >= 999) {
        this.pingText.setText('999ms')
      }
    }
  }

  Victory(piece) {
    this.status = -1
    let text = (piece.key == 'pieceBlack' ? '黑子方' : '白子方') + '胜出'
    setTimeout(() => {
      this.topCodeText.setText("游戏结束")
      this.bottomCodeText.setText(text)
    }, 100);

    this.game.Vue.notify(text)
  }
  // 退出
  exit() {

  }
  create() {
    this._drawPing()
    this._drawCheck()
    this.game.ws.send({
      func: "chessBoard.info",
      name: this.game.account.name
    }, (res) => {
      this.chessData = res
      this.isBlack = (res.chess[1] == this.game.account.name) ? 1 : 0
      this.chessBoard.loadData(this.chessData)
      this.updateText()
    })
    this.game.ws.listen('chessBoard.addRecord', (res) => {
      var [name, row, col] = res.tape
      this.chessBoard.createPiece(name, row, col)
    })

    this.markText = this.add.text(
      this.computedX(-this.chessBoard.checkBg.width/2 + 30),
      this.computedY(-this.chessBoard.checkBg.height - 60),
      '棋步', {
        fontSize: 26,
        fontFamily: '楷体',
        color: '#000000'
      }
    )
    this.markText.setOrigin(0.5,0.5)
    this.markText.setInteractive()
    this.markText.on('pointerdown',()=>{
      this.chessBoard.showMark()
    })

  }
  update() {

  }
}
